package com.leyou.search.mq;

import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static com.leyou.common.constants.MQConstants.Exchange.*;
import static com.leyou.common.constants.MQConstants.Queue.*;
import static com.leyou.common.constants.MQConstants.RoutingKey.*;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.search.mq
 * @date 2019/7/5 17:17
 * @description:
 **/
@Component
public class ItemListener {

    @Autowired
    private SearchService searchService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name=SEARCH_ITEM_UP,durable = "true"),
            exchange = @Exchange(
                    name=ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC
            ),
            key = ITEM_UP_KEY
    ))
    public void listenInsert(Long id){

        if (id != null) {
            //在索引库中新增索引
            searchService.createIndex(id);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name=SEARCH_ITEM_DOWN,durable = "true"),
            exchange = @Exchange(
                    name=ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC
            ),
            key = ITEM_DOWN_KEY
    ))
    public void listenDelete(Long id ){

        if (id!=null){
            //在索引库中删除索引
            searchService.deleteIndex(id);
        }

    }

}
